<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>abstractchannel.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_messaging_abstractchannel.js.html">abstractchannel.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2010 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview An abstract superclass for message channels that handles the
<a name="line17"></a> * repetitive details of registering and dispatching to services. This is more
<a name="line18"></a> * useful for full-fledged channels than for decorators, since decorators
<a name="line19"></a> * generally delegate service registering anyway.
<a name="line20"></a> *
<a name="line21"></a> */
<a name="line22"></a>
<a name="line23"></a>
<a name="line24"></a>goog.provide(&#39;goog.messaging.AbstractChannel&#39;);
<a name="line25"></a>
<a name="line26"></a>goog.require(&#39;goog.Disposable&#39;);
<a name="line27"></a>goog.require(&#39;goog.debug&#39;);
<a name="line28"></a>goog.require(&#39;goog.debug.Logger&#39;);
<a name="line29"></a>goog.require(&#39;goog.json&#39;);
<a name="line30"></a>goog.require(&#39;goog.messaging.MessageChannel&#39;); // interface
<a name="line31"></a>
<a name="line32"></a>
<a name="line33"></a>
<a name="line34"></a>/**
<a name="line35"></a> * Creates an abstract message channel.
<a name="line36"></a> *
<a name="line37"></a> * @constructor
<a name="line38"></a> * @extends {goog.Disposable}
<a name="line39"></a> * @implements {goog.messaging.MessageChannel}
<a name="line40"></a> */
<a name="line41"></a>goog.messaging.AbstractChannel = function() {
<a name="line42"></a>  goog.base(this);
<a name="line43"></a>
<a name="line44"></a>  /**
<a name="line45"></a>   * The services registered for this channel.
<a name="line46"></a>   * @type {Object.&lt;string, {callback: function((string|!Object)),
<a name="line47"></a>                             objectPayload: boolean}&gt;}
<a name="line48"></a>   * @private
<a name="line49"></a>   */
<a name="line50"></a>  this.services_ = {};
<a name="line51"></a>};
<a name="line52"></a>goog.inherits(goog.messaging.AbstractChannel, goog.Disposable);
<a name="line53"></a>
<a name="line54"></a>
<a name="line55"></a>/**
<a name="line56"></a> * The default service to be run when no other services match.
<a name="line57"></a> *
<a name="line58"></a> * @type {?function(string, (string|!Object))}
<a name="line59"></a> * @private
<a name="line60"></a> */
<a name="line61"></a>goog.messaging.AbstractChannel.prototype.defaultService_;
<a name="line62"></a>
<a name="line63"></a>
<a name="line64"></a>/**
<a name="line65"></a> * Logger for this class.
<a name="line66"></a> * @type {goog.debug.Logger}
<a name="line67"></a> * @protected
<a name="line68"></a> */
<a name="line69"></a>goog.messaging.AbstractChannel.prototype.logger =
<a name="line70"></a>    goog.debug.Logger.getLogger(&#39;goog.messaging.AbstractChannel&#39;);
<a name="line71"></a>
<a name="line72"></a>
<a name="line73"></a>/**
<a name="line74"></a> * Immediately calls opt_connectCb if given, and is otherwise a no-op. If
<a name="line75"></a> * subclasses have configuration that needs to happen before the channel is
<a name="line76"></a> * connected, they should override this and {@link #isConnected}.
<a name="line77"></a> * @override
<a name="line78"></a> */
<a name="line79"></a>goog.messaging.AbstractChannel.prototype.connect = function(opt_connectCb) {
<a name="line80"></a>  if (opt_connectCb) {
<a name="line81"></a>    opt_connectCb();
<a name="line82"></a>  }
<a name="line83"></a>};
<a name="line84"></a>
<a name="line85"></a>
<a name="line86"></a>/**
<a name="line87"></a> * Always returns true. If subclasses have configuration that needs to happen
<a name="line88"></a> * before the channel is connected, they should override this and
<a name="line89"></a> * {@link #connect}.
<a name="line90"></a> * @override
<a name="line91"></a> */
<a name="line92"></a>goog.messaging.AbstractChannel.prototype.isConnected = function() {
<a name="line93"></a>  return true;
<a name="line94"></a>};
<a name="line95"></a>
<a name="line96"></a>
<a name="line97"></a>/** @override */
<a name="line98"></a>goog.messaging.AbstractChannel.prototype.registerService =
<a name="line99"></a>    function(serviceName, callback, opt_objectPayload) {
<a name="line100"></a>  this.services_[serviceName] = {
<a name="line101"></a>    callback: callback,
<a name="line102"></a>    objectPayload: !!opt_objectPayload
<a name="line103"></a>  };
<a name="line104"></a>};
<a name="line105"></a>
<a name="line106"></a>
<a name="line107"></a>/** @override */
<a name="line108"></a>goog.messaging.AbstractChannel.prototype.registerDefaultService =
<a name="line109"></a>    function(callback) {
<a name="line110"></a>  this.defaultService_ = callback;
<a name="line111"></a>};
<a name="line112"></a>
<a name="line113"></a>
<a name="line114"></a>/** @override */
<a name="line115"></a>goog.messaging.AbstractChannel.prototype.send = goog.abstractMethod;
<a name="line116"></a>
<a name="line117"></a>
<a name="line118"></a>/**
<a name="line119"></a> * Delivers a message to the appropriate service. This is meant to be called by
<a name="line120"></a> * subclasses when they receive messages.
<a name="line121"></a> *
<a name="line122"></a> * This method takes into account both explicitly-registered and default
<a name="line123"></a> * services, as well as making sure that JSON payloads are decoded when
<a name="line124"></a> * necessary. If the subclass is capable of passing objects as payloads, those
<a name="line125"></a> * objects can be passed in to this method directly. Otherwise, the (potentially
<a name="line126"></a> * JSON-encoded) strings should be passed in.
<a name="line127"></a> *
<a name="line128"></a> * @param {string} serviceName The name of the service receiving the message.
<a name="line129"></a> * @param {string|!Object} payload The contents of the message.
<a name="line130"></a> * @protected
<a name="line131"></a> */
<a name="line132"></a>goog.messaging.AbstractChannel.prototype.deliver = function(
<a name="line133"></a>    serviceName, payload) {
<a name="line134"></a>  var service = this.getService(serviceName, payload);
<a name="line135"></a>  if (!service) {
<a name="line136"></a>    return;
<a name="line137"></a>  }
<a name="line138"></a>
<a name="line139"></a>  var decodedPayload =
<a name="line140"></a>      this.decodePayload(serviceName, payload, service.objectPayload);
<a name="line141"></a>  if (goog.isDefAndNotNull(decodedPayload)) {
<a name="line142"></a>    service.callback(decodedPayload);
<a name="line143"></a>  }
<a name="line144"></a>};
<a name="line145"></a>
<a name="line146"></a>
<a name="line147"></a>/**
<a name="line148"></a> * Find the service object for a given service name. If there&#39;s no service
<a name="line149"></a> * explicitly registered, but there is a default service, a service object is
<a name="line150"></a> * constructed for it.
<a name="line151"></a> *
<a name="line152"></a> * @param {string} serviceName The name of the service receiving the message.
<a name="line153"></a> * @param {string|!Object} payload The contents of the message.
<a name="line154"></a> * @return {?{callback: function((string|!Object)), objectPayload: boolean}} The
<a name="line155"></a> *     service object for the given service, or null if none was found.
<a name="line156"></a> * @protected
<a name="line157"></a> */
<a name="line158"></a>goog.messaging.AbstractChannel.prototype.getService = function(
<a name="line159"></a>    serviceName, payload) {
<a name="line160"></a>  var service = this.services_[serviceName];
<a name="line161"></a>  if (service) {
<a name="line162"></a>    return service;
<a name="line163"></a>  } else if (this.defaultService_) {
<a name="line164"></a>    var callback = goog.partial(this.defaultService_, serviceName);
<a name="line165"></a>    var objectPayload = goog.isObject(payload);
<a name="line166"></a>    return {callback: callback, objectPayload: objectPayload};
<a name="line167"></a>  }
<a name="line168"></a>
<a name="line169"></a>  this.logger.warning(&#39;Unknown service name &quot;&#39; + serviceName + &#39;&quot;&#39;);
<a name="line170"></a>  return null;
<a name="line171"></a>};
<a name="line172"></a>
<a name="line173"></a>
<a name="line174"></a>/**
<a name="line175"></a> * Converts the message payload into the format expected by the registered
<a name="line176"></a> * service (either JSON or string).
<a name="line177"></a> *
<a name="line178"></a> * @param {string} serviceName The name of the service receiving the message.
<a name="line179"></a> * @param {string|!Object} payload The contents of the message.
<a name="line180"></a> * @param {boolean} objectPayload Whether the service expects an object or a
<a name="line181"></a> *     plain string.
<a name="line182"></a> * @return {string|Object} The payload in the format expected by the service, or
<a name="line183"></a> *     null if something went wrong.
<a name="line184"></a> * @protected
<a name="line185"></a> */
<a name="line186"></a>goog.messaging.AbstractChannel.prototype.decodePayload = function(
<a name="line187"></a>    serviceName, payload, objectPayload) {
<a name="line188"></a>  if (objectPayload &amp;&amp; goog.isString(payload)) {
<a name="line189"></a>    try {
<a name="line190"></a>      return goog.json.parse(payload);
<a name="line191"></a>    } catch (err) {
<a name="line192"></a>      this.logger.warning(&#39;Expected JSON payload for &#39; + serviceName +
<a name="line193"></a>                          &#39;, was &quot;&#39; + payload + &#39;&quot;&#39;);
<a name="line194"></a>      return null;
<a name="line195"></a>    }
<a name="line196"></a>  } else if (!objectPayload &amp;&amp; !goog.isString(payload)) {
<a name="line197"></a>    return goog.json.serialize(payload);
<a name="line198"></a>  }
<a name="line199"></a>  return payload;
<a name="line200"></a>};
<a name="line201"></a>
<a name="line202"></a>
<a name="line203"></a>/** @override */
<a name="line204"></a>goog.messaging.AbstractChannel.prototype.disposeInternal = function() {
<a name="line205"></a>  goog.base(this, &#39;disposeInternal&#39;);
<a name="line206"></a>  delete this.logger;
<a name="line207"></a>  delete this.services_;
<a name="line208"></a>  delete this.defaultService_;
<a name="line209"></a>};
</pre>


</body>
</html>
